import { defineStore } from "pinia";
import { ref } from "vue";
import { menuOptions } from "@/mock/menu";
import { findParentWithChild } from "@/utils/findParentWithChildUtils";

// 定义Breadcrumb模块的store
export const useBreadcrumbStore = defineStore("breadcrumb", () => {
  const breadcrumbList = ref([
    {
      label: "工作台",
      key: "/home",
    },
  ]);

  //退出时清空数据
  const removeBreadcrumb = () => {
    breadcrumbList.value = [
      {
        label: "工作台",
        key: "/home",
      },
    ];
  };

  // 设置动态面包屑
  const setBreadcrumb = (newBreadcrumb) => {
    breadcrumbList.value = [];
    const result = findParentWithChild(menuOptions, newBreadcrumb.label);
    if (result !== null) {
      breadcrumbList.value.push({
        label: result.label,
        key: result.key,
      });
    }
    breadcrumbList.value.push({
      label: newBreadcrumb.label,
      key: newBreadcrumb.key,
    });
  };

  return {
    breadcrumbList,
    setBreadcrumb,
    removeBreadcrumb,
  };
});
